﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DataAccess;
using DataAccess.Classes;
using System.IO;

public partial class Admin_PhongTro : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            PopulateControls();
    }

    private void PopulateControls()
    {
        drDiaDiem.DataValueField = "ID";
        drDiaDiem.DataTextField = "TenDiaDiem";
        drDiaDiem.DataSource = DiaDiem.LayDanhSach();
        drDiaDiem.DataBind();
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        try
        {
            PhongTro data = new PhongTro();
            data.ID = Convert.ToInt32(drDiaDiem.SelectedValue.ToString());
            data.Ten = txtTen.Text;
            data.ThongTin = txtThongTin.Text;
            data.GiaTien = Convert.ToDecimal(txtGiaPhong.Text);
            data.HinhDaiDien = FileUpload1.FileName.ToString();
            data.HinhChinh = FileUpload1.FileName.ToString();
            if (cbTinhTrang.Checked == true)
                data.TinhTrang = Convert.ToBoolean("True".ToString());
            if (cbTinhTrang.Checked == false)
                data.TinhTrang = Convert.ToBoolean("False".ToString());
            data.IDNguoiDung = Session["id"].ToString();
            PhongTro.Them(data);
            lbThongBao.Text = "Thêm thành công!";
        }
        catch
        {
            lbThongBao.Text = "Thêm thất bại!";
        }
    }
    bool CheckFileType(string fileName)
    {
        string ext = Path.GetExtension(fileName);
        switch (ext.ToLower())
        {
            case ".gif":
                return true;
            case ".png":
                return true;
            case ".jpg":
                return true;
            case ".jpeg":
                return true;
            case ".bmp":
                return true;
            default:
                return false;
        }
    }
    protected void FileUpload1_Init(object sender, EventArgs e)
    {
        if (CheckFileType(FileUpload1.FileName) == true)
        {
            String filePath = "~/HinhPhongTro/" + FileUpload1.FileName;
            FileUpload1.SaveAs(MapPath(filePath));
        }
    }
}
